<?php

namespace app\admin\controller;

use app\admin\model\Account;
use app\admin\model\Admin;
use app\admin\model\Collect;
use app\admin\model\Device;
use app\common\controller\Backend;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    public function index()
    {
        set_time_limit(0);
        ini_set('memory_limit', '1024M');

        $groupIds = $this->request->get('groupIds');

        $adminIds = $this->auth->getChildrenAdminIds(true);
        $group_name = '全部';
        if($groupIds != ''){
            $groupIdsArr = $this->auth->getByGroupIds($groupIds);
            $adminIds = model('AuthGroupAccess')->where('group_id', 'in', $groupIdsArr)->column('uid');
            $group_name = db('auth_group')->where('id', $groupIds)->value('name');
        }
        
        $where['admin_id'] = ['in', $adminIds];

        $device = Device::where($where)->column('*', 'id');
        // 设备数量
        $device_count = count($device);

        // 设备账号数量
        $account = Account::where($where)->group('email')->field('count(*) as count')->select();
        $account_count = count($account);

        $today_data = Collect::where('type', 1)
            ->whereTime('date', 'today')
            ->where($where)
            ->field("sum(fans) fans,sum(play_num) play_num")
            ->find()->toarray();
        
        // 带货
        $today_count = Collect::where('type', 2)
            ->whereTime('date', 'today')
            ->where($where)
            ->field("sum(turnover) turnover,sum(amount) amount")
            ->find()->toarray();

        // 中视频
        $today_mid_video = Collect::where('type', 3)
            ->whereTime('date', 'today')
            ->where($where)
            ->field("sum(violent_num) violent_num,sum(amount) amount,sum(play_num) play_num")
            ->find()->toarray();

        $video_total_amount = Collect::where('type', 3)
            ->where($where)
            ->sum('amount');
        $limit = 30;
        // 粉丝排行榜
        $admin = Admin::column('nickname', 'id');
        $fansRank = Collect::where('type', 1)
            ->where($where)
            ->group('admin_id')
            ->field("admin_id,sum(fans) fans")
            ->order('fans desc')
            ->limit($limit)
            ->select();
        $colorArr = ['text-danger', 'text-info', 'text-warning'];
        foreach($fansRank as $key => $val){
            $val->rank = isset($colorArr[$key]) ? '<b class="' . $colorArr[$key] . '"> ' .( $key + 1) . ' </b>' : ( $key + 1) ;
            $val->nickname = $admin[$val->admin_id];
        }

        $turnoverRank = Collect::where('type', 2)
            ->where($where)
            ->group('admin_id')
            ->field("admin_id,sum(turnover) turnover")
            ->order('turnover desc')
            ->limit($limit)
            ->select();
        $total_turnover = 0;
        foreach($turnoverRank as $key => $val){
            $val->rank = isset($colorArr[$key]) ? '<b class="' . $colorArr[$key] . '"> ' .( $key + 1) . ' </b>' : ( $key + 1) ;
            $val->nickname = $admin[$val->admin_id];
            $total_turnover += $val->turnover;
        }

        $amountRank = Collect::where('type', 2)
            ->where($where)
            ->group('admin_id')
            ->field("admin_id,sum(amount) amount")
            ->order('amount desc')
            ->limit($limit)
            ->select();
        $total_amount = 0;
        foreach($amountRank as $key => $val){
            $val->rank = isset($colorArr[$key]) ? '<b class="' . $colorArr[$key] . '"> ' .( $key + 1) . ' </b>' : ( $key + 1) ;
            $val->nickname = $admin[$val->admin_id];
            $total_amount += $val->amount;
        }

        $mid_video_amountRank = Collect::where('type', 3)
            ->where($where)
            ->group('admin_id')
            ->field("admin_id,sum(amount) amount")
            ->order('amount desc')
            ->limit($limit)
            ->select();
        foreach($mid_video_amountRank as $key => $val){
            $val->rank = isset($colorArr[$key]) ? '<b class="' . $colorArr[$key] . '"> ' .( $key + 1) . ' </b>' : ( $key + 1) ;
            $val->nickname = $admin[$val->admin_id];
        }

        $retval = [
            'device_count'          => $device_count,
            'account_count'         => $account_count,
            'total_turnover'        => $total_turnover,
            'total_amount'          => $total_amount,
            'today_data'            => $today_data,
            'today_count'           => $today_count,
            'today_mid_video'       => $today_mid_video,
            'video_total_amount'    => $video_total_amount,
        ];
        $this->assign('turnoverRank', $turnoverRank);
        $this->assign('amountRank', $amountRank);
        $this->assign('fansRank', $fansRank);
        $this->assign('mid_video_amountRank', $mid_video_amountRank);
        $this->assign('limit', $limit);
        $this->assign('retval', $retval);
        $this->assign('group_name', $group_name);
        return $this->view->fetch();
    }
}
